<%@ page pageEncoding="UTF-8" %>
<!doctype html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css"
          integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.css">
    <link href="https://cdn.bootcdn.net/ajax/libs/toastr.js/2.1.4/toastr.min.css" rel="stylesheet">
    <title>随便打TCS系统</title>
</head>

<body class="nav-md">
<div>
    <div class="container-fluid" style="padding-left: 235px">
        <div>
            <div id="toolbar" class="btn-group">
                <div class="row">
                    <div class="col-14" style="margin-left: 15px">
                        <form class="form-inline" id="formSelect">
                            <div class="form-group mb-2">
                                <input type="text" class="form-control" id="userMobileSelect" placeholder="用户手机号">
                            </div>
                            <div class="form-group mb-2">
                                <button type="button" id="search" class="btn btn-success">查询</button>
                                <button type="reset" id="reset" class="btn btn-danger">重置</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <table id="table"></table>
        </div>

        <!-- 修改 -->
        <div class="modal fade" id="updateShow" tabindex="-1" role="dialog" aria-labelledby="updateShowTitle">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title" id="updateShowTitle">修改商品</h4>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    </div>
                    <div class="modal-body">
                        <form id="formUpdate">
                            <input type="text" hidden class="form-control" id="idUpdate"/>
                            <div class="form-row">
                                <div class="form-group col-md-6">
                                    <label for="mobileUpdate">收件人手机号：</label>
                                    <input type="text" class="form-control" id="mobileUpdate" placeholder="请输入收件人手机号">
                                </div>
                                <div class="form-group col-md-6">
                                    <label for="nameUpdate">收件人姓名：</label>
                                    <input type="text" class="form-control" id="nameUpdate" placeholder="请输入收件人姓名">
                                </div>
                            </div>
                            <div class="form-row">

                            </div>
                            <div class="form-row">
                                <label for="addressUpdate">详细地址：</label>
                                <div class="form-group" style="width: 100%;">
                                    <textarea class="form-control" id="addressUpdate" rows="3"></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" id="updatePost">修改</button>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

</body>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"
        integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo"
        crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/js/bootstrap.min.js"
        integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI"
        crossorigin="anonymous"></script>
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/bootstrap-table/1.15.3/locale/bootstrap-table-zh-CN.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/toastr.js/2.1.4/toastr.min.js"></script>
</html>
<script>
    $(function () {
        $(document).ajaxComplete(function (event, xhr, settings) {
            let url = xhr.getResponseHeader("redirectUrl");

            let enable = xhr.getResponseHeader("enableRedirect");

            if((enable === "true") && (url !== "")){
                let win = window;
                while(win !== win.top){
                    win = win.top;
                }
                win.location.href = url;
            }
        })
        Date.prototype.Format = function (fmt) {
            const o = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S": this.getMilliseconds()
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (let k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };

        window.operateEvents = {
            "click #showUpdateId": function (e, value, row, index) {
                $('#formUpdate')[0].reset();
                $('#idUpdate').val(row.id);
                $('#mobileUpdate').val(row.mobile);
                $('#nameUpdate').val(row.name);
                $('#addressUpdate').val(row.address);
                $('#updateShow').modal('show');
            }
        };

        function actionFormatter(value, row, index) {
            return [
                '<button id="showUpdateId" type="button" class="btn btn-warning">修改</button>'
            ].join('');
        }


        function formatDate(value, row, index) {
            return new Date(parseInt(value)).Format("yyyy-MM-dd hh:mm:ss");
        }

        const columns = [
            // {checkbox: true, 'data-halign': 'center', width: 50},
            {field: 'userMobile', title: '用户手机号', align: 'left', width: 150},
            {field: 'mobile', title: '收件人手机号', align: 'left', width: 150},
            {field: 'name', title: '收件人姓名', align: 'left', width: 272},
            {field: 'province', title: '省', align: 'left', width: 150},
            {field: 'city', title: '市', align: 'left', width: 150},
            {field: 'district', title: '区', align: 'left', width: 150},
            {field: 'address', title: '详细地址', align: 'left', width: 150},
            {field: 'createTime', title: '创建时间', align: 'left', width: 272, formatter: formatDate},
            {title: '操作', align: 'center', formatter: actionFormatter, events: operateEvents, width: 300}
        ];
        const bootstrapTable = {
            url: '/SbdVoip/admin/selfAddress/selfAddressWebList',
            pagination: true,
            toolbar: '#toolbar',
            smartDisplay: false,
            showRefresh: true,
            height: $(window).height(),
            method: 'post',
            pageList: "[10, 15, 20]",
            search: false,
            contentType: 'application/x-www-form-urlencoded',
            dataType: 'json',
            sidePagination: 'server',
            pageNumber: 1,
            pageSize: 10,
            onlyInfoPagination: false,
            striped: true,
            columns: columns,
            onPostHeader: function () {
                $('#tableId').bootstrapTable('resetView');
            },
            queryParams: function (params) {
                return {
                    offset: (params.offset / params.limit) + 1,
                    limit: params.limit
                };
            },
            responseHandler: function (res) {
                if (res.code !== 2000) {
                    toastr.error(res.message);
                    return {
                        total: 0,
                        rows: []
                    }
                }
                return {
                    total: res.data.total,
                    rows: res.data.rows
                }
            }
        };
        $('#table').bootstrapTable(bootstrapTable);
        $('#search').click(function () {
            const userMobile = $('#userMobileSelect').val();
            bootstrapTable.queryParams = function (params) {
                return {
                    offset: (params.offset / params.limit) + 1,
                    limit: params.limit,
                    userMobile: userMobile
                };
            };
            const table = $('#table');
            table.bootstrapTable('destroy');
            table.bootstrapTable(bootstrapTable);
        });


        $("#updatePost").click(function () {
            const data = {
                id: $('#idUpdate').val(),
                mobile: $('#mobileUpdate').val(),
                name: $('#nameUpdate').val(),
                address: $('#addressUpdate').val()
            };
            $.ajax({
                url: "/SbdVoip/admin/selfAddress/selfAddressWebUpdate",
                type: "POST",
                data: data,
                dataType: "json",
                contentType: "application/x-www-form-urlencoded",
                success: function (result) {
                    if (result.code !== 2000) {
                        toastr.error(result.message);
                        return
                    }
                    const updateShow = $('#updateShow');
                    updateShow.modal('hide');
                    const table = $('#table');
                    table.bootstrapTable('destroy');
                    table.bootstrapTable(bootstrapTable);
                    toastr.success(result.message);
                }
            });
        });
    });
</script>
